#pragma once
#include <iostream>
#include <string>

namespace Myprotocol
{
    class Request
    {
    public:
        Request(const int a = 0, const int b = 0, const char op = 0)
            : _a(a), _b(b), _op(op)
        {
        }
        std::string serialize()
        {
            // len\n_a\n_op\n_b\n
            std::string str;
            str += std::to_string(_a);
            str += '\n';
            str += _op;
            str += '\n';
            str += std::to_string(_b);
            str += '\n';
            int len = str.size();
            std::string ret;
            ret += std::to_string(len);
            ret += '\n';
            ret += str;

            return ret;
        }

        std::string deserialize(const std::string &str)
        {
            std::string tmp = str;
            std::string ret;
            // len\n_a\n_op\n_b\n -> _a _op _b
            int pos = tmp.find('\n');
            if (pos == std::string::npos)
                return "";
            int len = std::stoi(tmp.substr(0, pos));
            tmp.erase(0, pos + 1);
            if (len != tmp.size())
                return "";

            while (!tmp.empty())
            {
                int pos = tmp.find('\n');
                if (pos == std::string::npos)
                    return "";
                ret += tmp.substr(0, pos);
                tmp.erase(0, pos + 1);
            }
            return ret;
        }

    private:
        int _a;
        int _b;
        char _op;
    };

    class response
    {
    public:
        response(const int ret,const int code)
            :_ret(ret),_code(code)
        {}
    private:
        int _ret;
        int _code;
    };
}